﻿/*********************************************************************************
**                                                                              **
**  Copyright (C) 2024 LiLong                                                   **
**  This file is part of RinexFileReader.                                       **
**                                                                              **
**  RinexFileReader is free software: you can redistribute it and/or modify     **
**  it under the terms of the GNU General Public License as published by        **
**  the Free Software Foundation, either version 3 of the License, or           **
**  (at your option) any later version.                                         **
**                                                                              **
**  RinexFileReader is distributed in the hope that it will be useful,          **
**  but WITHOUT ANY WARRANTY; without even the implied warranty of              **
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               **
**  GNU General Public License for more details.                                **
**                                                                              **
**  You should have received a copy of the GNU General Public License           **
**  along with RinexFileReader. If not, see <https://www.gnu.org/licenses/>.    **
**********************************************************************************/
#include "RinexFile.h"
#include "Private/MeteorologicalData.h"
#include "Private/NavigationMessageData.h"
#include "Private/ObservationData.h"

#include <print>

namespace Rinex
{
std::expected<RindexFile, std::string> RindexFile::parse(const std::string& content)
{
    try
    {
        if (auto od = std::make_shared<ObservationData>();
            tao::pegtl::parse<PEG::Observation::Rule, PEG::Observation::Action>(tao::pegtl::string_input(content, ""), *od))
        {
            return ObservationDataFile(od);
        }
        if (auto nmd = std::make_shared<NavigationMessageData>();
            tao::pegtl::parse<PEG::NavigationMessage::Rule, PEG::NavigationMessage::Action>(tao::pegtl::string_input(content, ""), *nmd))
        {
            return NavigationMessageFile(nmd);
        }
        if (auto md = std::make_shared<MeteorologicalData>();
            tao::pegtl::parse<PEG::Meteorological::Rule, PEG::Meteorological::Action>(tao::pegtl::string_input(content, ""), *md))
        {
            return MeteorologicalDataFile(md);
        }
        return std::unexpected("Parse failed.");
    }
    catch (const std::exception& e)
    {
        // std::println("{}", e.what());
        return std::unexpected(e.what());
    }
}

} // namespace Rinex
